<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Vova_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    function getListTitleNew() {
        $this->load->database();
        $sql = "SELECT id, title FROM t_vova WHERE `check` = 1 ORDER BY id DESC LIMIT 15";
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    function getVovaById($id, $column = '*') {
        if (isset($id)) {
            $sql = "SELECT $column FROM t_vova WHERE id = ?";
            $query = $this->db->query($sql, array($id));
            if ($query->num_rows() < 1) {
                return null;
            }
        } else {
            $sql = "SELECT $column FROM t_vova WHERE `check` = 1 ORDER BY id DESC LIMIT 1";
            $query = $this->db->query($sql, array($id));
        }

        return $query->row_array();
    }

    function getIdRandomNotByArray ($params = null) {
        $sql = "SELECT id FROM `t_vova` WHERE `check` = 1 ORDER BY RAND() LIMIT 1;";
        $query = $this->db->query($sql);
        return $query->row()->id;
    }

    function getListVova($limit, $offset, $query_array) {
        if (strlen($query_array['search'])) {
            $this->db->like('title', $query_array['search']);
        }
        if (strlen($query_array['check']) && $query_array['check'] > 0) {
            $value = $this->_getCheckBySearch($query_array['check']);
            $this->db->where('check', $value);
        }

        $this->db->order_by("id", "desc");
        $query = $this->db->get('t_vova', $limit, $offset);

        return $query->result();
    }

    function getCountVova($query_array) {
        if (strlen($query_array['search'])) {
            $this->db->like('title', $query_array['search']);
        }
        if (strlen($query_array['check']) && $query_array['check'] > 0) {
            $value = $this->_getCheckBySearch($query_array['check']);
            $this->db->where('check', $value);
        }

        $this->db->from('t_vova');
        $count = $this->db->count_all_results();

        return $count;
    }

    private function _getCheckBySearch($checkSearch) {
        switch ($checkSearch) {
            case 1:
                return 1;
            break;
            case 2:
                return 0;
            break;
            case 3:
                return 2;
            break;
        }
    }

    function insert_vova() {
        $data = array(
            'title' => $this->input->post('title'),
            'content' => $this->input->post('content'),
            'character' => $this->input->post('character'),
            'check' => 1,
        );

        return $this->db->insert('t_vova', $data);
    }

    function insert_tools_vova($arrData) {
        return $this->db->insert_batch('t_vova', $arrData);
    }

    function update_vova($id) {
        $data = array(
                'title' => $this->input->post('title'),
                'content' => $this->input->post('content'),
                'character' => $this->input->post('character'),
                'check' => 1,
        );
        $this->db->where('id', $id);
        return $this->db->update('t_vova', $data);
    }

    function delete_vova($id) {
        return $this->db->delete('t_vova', array('id' => $id)); 
    }

    function getAdminVovaById($id, $column = '*') {
        $query = $this->db->get_where('t_vova', array('id' => $id));
        return $query->row_array();
    }

    function getAdminVovaCheck($arrIds = null, $check = 0) {
        if (!empty($arrIds)) {
            $this->db->where_not_in('id', $arrIds);
        }
        $query = $this->db->get_where('t_vova', array('check' => $check));
        return $query->row();
    }

    function update_check_vova($id, $check = 1) {
        $data = array(
            'check' => $check,
        );
        $this->db->where('id', $id);
        return $this->db->update('t_vova', $data);
    }
}